package com.zhixue.media.runtime;

import io.minio.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.concurrent.*;

/**
 * @Auther: 李 力
 * @Date: 2025/4/2
 * @Description: com.lili.minioserver.runtime
 * @version: 1.0
 */
@Component
public class MinioBucketRuntime implements CommandLineRunner {

    @Autowired
    private MinioClient minioClient;

    @Value("${minio.bucket.files}")
    private String files;

    @Value("${minio.bucket.videofiles}")
    private String videofiles;


    public void initUserBucket() {
        try {
            boolean result = minioClient.bucketExists(BucketExistsArgs.builder()
                    .bucket(files).build()
            );
            if (!result) {
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(files).build());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void initContractBucket() {
        try {
            boolean result = minioClient.bucketExists(BucketExistsArgs.builder()
                    .bucket(videofiles).build()
            );
            if (!result) {
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(videofiles).build());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public void run(String... args) throws Exception {
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.submit(this::initUserBucket);
        executorService.submit(this::initContractBucket);
    }
}
